---
title: "Recievers' Coding Check"
author: "Group"
date: "2024-05-28"
output: pdf_document
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE,message=FALSE)

```

```{r, include=FALSE}
rm(list=ls())
library(dplyr)
library(data.table) 
```

### Read in data for receivers
```{r}

setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/1_rawdata")

df <- read.csv("receivers_raw.csv")
df<- df[-c(1:3), ] #Removes first three junk columns
```


```{r}
library(dplyr)
### Rename Variables ### 
df <- df %>% 
  rename( 
         partyid_5 = Q5.1, 
         skip = Q46, 
         respid_env = resp_id_env,
         respid_trans = resp_id_trans,
         respid_imm= resp_id_imm,
         post_imm = Q6.3_1,
         post_trans = Q7.3_1,
         post_env= Q8.3_1,
         pre_imm = Q2.1_1,
         pre_trans = Q3.1_1,
         pre_env= Q4.1_1,
         reasoning_env = Q8.4, 
         clarity_env = Q8.5, 
         persuasive_env = Q8.6,
         reasoning_imm = Q6.4,  
         clarity_imm = Q6.5, 
         persuasive_imm = Q6.6, 
         reasoning_trans = Q7.4, 
         clarity_trans = Q7.5, 
         persuasive_trans = Q7.6, 
         repub_strength = Q5.2,
         demo_strength = Q5.3,
         party_lean = Q5.4, 
         OE_persuasion = Q9.1, 
         comments = Q10.1
         ) %>% 
  mutate(
    across(c(pre_env, post_env, pre_imm, post_imm, pre_trans, post_trans,
                 api_env, api_imm, api_trans, partyid_5), as.numeric) 
    )

# Create new dataframe
df2 <- df 
```

```{r}
## Create a seven-point partisanship variable ranging from 1 (strong Democrat) to 7 (Strong Republican)

df2 <- df2 %>% 
  mutate(
    partyid_7 = case_when(
      demo_strength == 1 ~ 1, 
      demo_strength == 2 ~ 2, 
      party_lean == 2 ~ 3, 
      party_lean == 3 ~ 4, 
      party_lean == 1 ~ 5, 
      repub_strength == 2 ~ 6, 
      repub_strength == 1 ~ 7, 
      TRUE ~ NA_integer_
    )
  )

```

```{r}
#Create issueside_env, issueside_imm, issueside_trans variables
#denoting Receiver's initial stance (whether liberal, neutral, or conservative)

# Make a variable indicating the respondent's issue side on the environment
# called issueside_env, where 0-4 (conservative = 3), 5 (neutral = 2), 6-10 (liberal = 1). 

# Make another variable called issueside_imm indicating the respondent's issue side on
# on the immigration where 0-4 (conservative = 3), 5 (neutral = 2), 6-10 (liberal = 1)

# Finally, make a variable called issueside_trans indicating the respondent's issue side on 
# transgender rights. Unlike, the previous two issues, a score of 0-4 indicates a liberal position
# a score of 5 indicates a neutral position, and a score of 6-10 indicates a conservative position. 
# To account for this reverse code the pre_trans and post_trans opinion variables and create a 
# new variable called issueside_trans where a score of 0-4 is now conservative (= 3), 5 is still 
# neutral (= 2) and a score ranging from 6-10 is liberal (= 1)

df2 <- df2 %>% 
  mutate(
    across(c(pre_trans, post_trans), ~recode(as.numeric(.),
                                             "0" = 10, 
                                             "1" = 9, 
                                             "2" = 8, 
                                             "3" = 7, 
                                             "4" = 6, 
                                             "5" = 5, 
                                             "6" = 4, 
                                             "7" = 3,
                                             "8" = 2,
                                             "9" = 1, 
                                             "10" = 0))
    ) %>% 
  mutate(
    issueside_env = case_when(
      pre_env > 5 ~ 1, 
      pre_env == 5 ~ 2, 
      pre_env < 5 ~ 3, 
      TRUE ~ NA_integer_
    ), 
    issueside_imm = case_when(
      pre_imm > 5 ~ 1, 
      pre_imm == 5 ~ 2, 
      pre_imm < 5 ~ 3, 
      TRUE ~ NA_integer_
    ), 
    issueside_trans = case_when(
      pre_trans > 5 ~ 1, 
      pre_trans == 5 ~ 2, 
      pre_trans < 5 ~ 3, 
      TRUE ~ NA_integer_
    )
  ) %>% 
  mutate(
    across(c(issueside_env, issueside_imm, issueside_trans), as.factor)
  ) 


```

## Argument Side: 
```{r}
# Classify the ideological direction of the argument read by Receivers.
# We systematically assigned each argument a unique API code (api_env, api_imm, api_trans)
# which we used for randomizatation in Qualtrics
# and which we use here to code the argument's side.

### Environment ###
# api 1:51 are conservative arguments (2) and 
# api 52:169 are liberal arguments (1)

### Immigration ## 
# api 170:215 are conservative arguments (2)
# api 216:299 are liberal arguments (1)

### Transgender Rights ### 
# api 300:352 are conservative arguments (2)
# api 353:400 are liberal arguments (1)


df2 <- df2 %>% 
  mutate(
    across(c(api_env, api_imm, api_trans), as.numeric) 
  ) %>% 
  mutate(
    argueside_env = case_when(
      api_env >= 52 ~ 1, 
      api_env <= 51 ~ 2, 
      TRUE ~ NA_integer_
      ), 
    argueside_imm = case_when(
      api_imm >= 216 ~ 1,  
      api_imm <= 215 ~ 2, 
      TRUE ~ NA_integer_
      ), 
    argueside_trans = case_when(
      api_trans >= 353 ~ 1, 
      api_trans <= 352 ~ 2, 
      TRUE ~ NA_integer_
      )
    ) %>% 
  mutate(
    across(c(argueside_env, argueside_imm, argueside_trans), as.factor)
    ) 



```

# Argument Movement
```{r}

#Creating variables (quarkelt 1 and quarkelt 2) defined by the change in Receiver issue opinion (post-argument exposure compared to pre-). 
# coded such that positive values represent change in the Sender’s intended direction
# and negative values represent “backfire” (the Receiver moved away from the Sender’s opinion on the issue).

# I.e. a positive change in time1 to time2 score always indicates movement in the direction 
# of the argument.

# For instance, a respondent with a liberal response (6-10), will see a conservative 
# argument. A lower number at time 2 would indicate movement in the direction of the 
# the argument but the T2 to T1 difference would be negative. Accordingly, Liberals would 
# receive a T1 score of 10 - pre_issueopinion and a T2 score of 10 - post_issueopinion. 
# In this way, if a respondent, for instance, answers an 8, they will receive a T1 score 
# of 2, and if they move in the conservative (intended) direction to say, a 6, they will receive a T2
# score of 4. Now a difference between T2 and T1 would be positive indicating movement in the direction 
# of the argument.If a liberal respondent moves from 8 to 9, however
# then the T2 - T1 difference would be negative
# (10-9) - (10-8)=1-2=-1 (indicating a backlash)


df2 <- df2 %>% 
  mutate(
    quarkelt1_env = case_when(
      pre_env <= 5 ~ pre_env, # Conservatives and Neutrals
      pre_env > 5 ~ 10 - pre_env, # Liberals
      TRUE ~ NA_integer_
    ), 
    quarkelt2_env = case_when(
      pre_env < 5 ~ post_env, # Conservatives 
      pre_env > 5 ~ 10 - post_env, # Liberals
      pre_env == 5 & argueside_env == 1 ~ post_env, # Neutrals who saw a liberal argument
      pre_env == 5 & argueside_env == 2 ~ 10 - post_env, # Neutrals who saw a conservative argument
      TRUE ~ NA_integer_
    ), 
    quarkelt1_imm = case_when(
      pre_imm <= 5 ~ pre_imm, # Conservatives and Neutrals
      pre_imm > 5 ~ 10 - pre_imm, # Liberals
      TRUE ~ NA_integer_
    ), 
    quarkelt2_imm = case_when(
      pre_imm < 5 ~ post_imm, # Conservatives 
      pre_imm > 5 ~ 10 - post_imm, # Liberals
      pre_imm == 5 & argueside_imm == 1 ~ post_imm, # Neutrals who saw a liberal argument
      pre_imm == 5 & argueside_imm == 2 ~ 10 - post_imm, # Neutrals who saw a conservative argument
      TRUE ~ NA_integer_
    ), 
    quarkelt1_trans = case_when(
      pre_trans <= 5 ~ pre_trans, # Conservatives and Neutrals (Because these variables have been reverse coded)
      pre_trans > 5 ~ 10 - pre_trans, #Liberals
      TRUE ~ NA_integer_
    ), 
    quarkelt2_trans = case_when(
      pre_trans < 5 ~ post_trans, # Conservatives
      pre_trans > 5 ~ 10 - post_trans, # Liberals
      pre_trans == 5 & argueside_trans == 1 ~ post_trans, # Neutrals who saw a liberal argument 
      pre_trans == 5 & argueside_trans == 2 ~ 10 - post_trans, # Neutrals who saw a conservative argument
      TRUE ~ NA_integer_
    )
  )


```


```{r}
## A binary version of the DV where movement in the direction of 
## the argument = 1 and no movement and movement away from the argument 
# (backlash) = 0

df2 <- df2 %>% 
  mutate(
    binary_env = case_when(
      quarkelt2_env > quarkelt1_env ~ 1, 
      quarkelt2_env <= quarkelt1_env ~ 0, 
      TRUE ~ NA_integer_
      ), 
    binary_imm = case_when(
      quarkelt2_imm > quarkelt1_imm ~ 1, 
      quarkelt2_imm <= quarkelt1_imm ~ 0, 
      TRUE ~ NA_integer_
      ), 
    binary_trans = case_when(
      quarkelt2_trans > quarkelt1_trans ~ 1, 
      quarkelt2_trans <= quarkelt2_trans ~ 0, 
      TRUE ~ NA_integer_)
    ) %>% 
  mutate(
    across(c(binary_env, binary_imm, binary_trans), as.factor)
  )



```


```{r}
## Persuasion (movement in Sender's intended direction) = 1
# No movement = 0
# Backlash (movement away from the argument) = -1

df2 <- df2 %>% 
  mutate(
    movement_env = case_when(
      quarkelt2_env > quarkelt1_env ~ 1, 
      quarkelt2_env == quarkelt1_env ~ 0, 
      quarkelt2_env < quarkelt1_env ~ -1, 
      TRUE ~ NA_integer_
    ), 
    movement_imm = case_when(
      quarkelt2_imm > quarkelt1_imm ~ 1, 
      quarkelt2_imm == quarkelt1_imm ~ 0, 
      quarkelt2_imm < quarkelt1_imm ~ -1,
      TRUE ~ NA_integer_
    ), 
    movement_trans = case_when(
      quarkelt2_trans > quarkelt1_trans ~ 1, 
      quarkelt2_trans == quarkelt1_trans ~ 0, 
      quarkelt2_trans < quarkelt1_trans ~ -1, 
      TRUE ~ NA_integer_
    )
  ) %>% 
  mutate(
    across(c(movement_env, movement_imm, movement_trans), as.factor)
  )


```


```{r}
summary(df2$movement_env)
# -1    0    1 NA's 
# 108  591  252   75 

# 118 + 574 + 202 = 2781

summary(df2$movement_imm)
#  -1    0    1 NA's 
# 101  617  218   90 

summary(df2$movement_trans)
#  -1    0    1 NA's 
# 101  574  219  132 

```

```{r}
df2$respondent <- as.numeric(seq.int(nrow(df2)))

```

```{r}
##########
# EXPORT #
########## 
# Specify the folder path
setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata")

# Specify the folder path
folder_path_receivers_1 <- "/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata/"


# Create the full file path including the filename and extension 
file_path <- file.path(folder_path_receivers_1, "receivers_1_working_wide_v2.csv")

# Export the dataframe to CSV 
write.csv(df2, file = file_path, row.names = FALSE)

# Check if file exists 
file.exists(file_path) 

```




